import moment from 'moment'
import { $get, $put, $post } from '@js/authRequest'
import { formatCaseDataLicense } from '@src/assets/js/tool'

// 获取字典信息
export async function getDictinarys () {
  const res = await $get('/api/xapc/dictionary', {
    types: '案件大类,涉外情况,仲裁程序,案件来源,主体类型,自然人证件类型,企业证件类型,所在地,金融业企业类型,案件涉外类型,受理案件类型'
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return formatDictionaries(res.data)
}

// 格式化枚举字典
function formatDictionaries (dictionaries = []) {
  const result = {}
  dictionaries.forEach(d => {
    result[d.type] = d.list
  })
  return result
}

// 获取案件类型树
export async function getCaseTypeTree () {
  const res = await $get('/api/xapc/dictionary/getTreeByType', {
    type: '案件分类'
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return formatCaseTypeTree(res.data)
}

// 格式化案件类型树对象
function formatCaseTypeTree (list, titles = []) {
  return list.map(li => {
    li.titles = [...titles, li.title]
    if (li.children && li.children.length) {
      li.children = formatCaseTypeTree(li.children, li.titles)
    } else {
      delete li.children
    }
    return li
  })
}

// 获取案件数据
export async function getCaseDetail (id) {
  const res = await $get(`/api/xapc/case/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  formatCaseDataLicense(res.data)
  return formatCaseDetail(res.data)
}

// 格式化案件数据
function formatCaseDetail (d = {}) {
  d.accept_datetime = d.accept_datetime ? moment(d.accept_datetime * 1000) : undefined
  if (!d.claim) {
    d.claim = { items: [] }
  }
  const { claim } = d
  if (!claim.dispute_amount) {
    d.dispute_amount = undefined
  }
  if (!claim.fee_type) {
    claim.fee_type = '普通收费'
  }
  setObjectNullToUndefine(d)
  return d
}

// 将对象中为null属性值转化为undefine
function setObjectNullToUndefine (o) {
  const keys = Object.keys(o)
  keys.forEach(k => {
    if (!o[k]) {
      o[k] = undefined
    }
  })
}

// 获取请求项配置
export async function getClaimItemConfig () {
  const res = await $get('/api/xapc/claimitemconfig')
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return formatCaseTypeTree(res.data)
}

// 获取案由树
export async function getCauseActionTree () {
  const res = await $get('/api/common/cause_action')
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 更新案件详情
export async function updateCaseRequest (caseid, data) {
  const res = await $put(`/api/xapc/case/${caseid}`, data)
  if (res.status === 2) {
    return res
  }
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return false
}

// 创建新案件
export async function createCaseRequest () {
  const res = await $post('/api/xapc/case')
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 根据案由获取标签
export async function getTagByAction (causeaction) {
  const res = await $get('/api/common/causeaction/getTagByCauseaction', {
    causeaction
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 审核不通过
export async function unpassRequest (caseid, status, remark) {
  const res = await $put('/api/xapc/case/caseSubmitCheck', {
    caseid,
    status,
    remark
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}
